package cn.edu.nju.software.greencreditsystem.DAOImpl;

import cn.edu.nju.software.greencreditsystem.DAO.EnterpriseDAO;
import cn.edu.nju.software.greencreditsystem.DAO.EnvironmentFactorDAO;
import cn.edu.nju.software.greencreditsystem.business.CoinEnvironmentAnalyser;
import cn.edu.nju.software.greencreditsystem.domain.Enterprise;
import cn.edu.nju.software.greencreditsystem.domain.EnvironmentExcel;
import cn.edu.nju.software.greencreditsystem.domain.EnvironmentStandardValue;
import cn.edu.nju.software.greencreditsystem.domain.FinancialExcel;
import cn.edu.nju.software.greencreditsystem.factory.DAOFactory;
import cn.edu.nju.software.greencreditsystem.factory.SessionPool;
import java.util.ArrayList;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;

/**
 * 作者：何启睿
 * 创建时间：2011-8-2
 * 类描述：Enterprise类DAO实现
 */
public class EnterpriseDAOImpl implements EnterpriseDAO
{

    @Override
    public boolean save(Enterprise e) 
    {
        boolean result = false;
        
        Session s = SessionPool.getSession();
        try
        {
            s.beginTransaction();
            s.save(e);
            
            //添加企业时同时添加默认环境标准值
            EnvironmentStandardValue esv[] = new EnvironmentStandardValue[CoinEnvironmentAnalyser.indexNames.length];
            EnvironmentFactorDAO efdao = DAOFactory.getEnvironmentFactorDAOInstance();
            for(int i = 0; i < esv.length; i++)
            {
                esv[i] = new EnvironmentStandardValue();
                esv[i].setEnterprise(e);
                esv[i].setFactor(efdao.getByName(CoinEnvironmentAnalyser.indexNames[i]));
            }
            esv[0].setChangeValue(50);
            esv[0].setStandardValue(34);
            esv[0].setType(0);
            esv[1].setChangeValue(50);
            esv[1].setStandardValue(13);
            esv[1].setType(0);
            esv[2].setChangeValue(50);
            esv[2].setStandardValue(13);
            esv[2].setType(0);
            esv[3].setChangeValue(50);
            esv[3].setStandardValue(13);
            esv[3].setType(0);
            esv[4].setChangeValue(50);
            esv[4].setStandardValue(26);
            esv[4].setType(0);
            esv[5].setChangeValue(50);
            esv[5].setStandardValue(100);
            esv[5].setType(0);
            esv[6].setChangeValue(50);
            esv[6].setStandardValue(100000);
            esv[6].setType(-1);
            esv[7].setChangeValue(50);
            esv[7].setStandardValue(500000);
            esv[7].setType(-1);
            esv[8].setChangeValue(50);
            esv[8].setStandardValue(50000);
            esv[8].setType(-1);
            esv[9].setChangeValue(50);
            esv[9].setStandardValue(500000);
            esv[9].setType(-1);
            esv[10].setChangeValue(50);
            esv[10].setStandardValue(50000);
            esv[10].setType(-1);
            esv[11].setChangeValue(50);
            esv[11].setStandardValue(3000);
            esv[11].setType(-1);
            esv[12].setChangeValue(50);
            esv[12].setStandardValue(3000);
            esv[12].setType(-1);
            esv[13].setChangeValue(50);
            esv[13].setStandardValue(3000);
            esv[13].setType(-1);
            esv[14].setChangeValue(50);
            esv[14].setStandardValue(200000);
            esv[14].setType(-1);
            esv[15].setChangeValue(50);
            esv[15].setStandardValue(200000);
            esv[15].setType(-1);
            esv[16].setChangeValue(50);
            esv[16].setStandardValue(200000);
            esv[16].setType(-1);
            esv[17].setChangeValue(50);
            esv[17].setStandardValue(200000);
            esv[17].setType(-1);
            esv[18].setChangeValue(50);
            esv[18].setStandardValue(200000);
            esv[18].setType(-1);
            esv[19].setChangeValue(50);
            esv[19].setStandardValue(200000);
            esv[19].setType(-1);
            esv[20].setChangeValue(50);
            esv[20].setStandardValue(20);
            esv[20].setType(-1);
            esv[21].setChangeValue(50);
            esv[21].setStandardValue(20);
            esv[21].setType(-1);
            esv[22].setChangeValue(50);
            esv[22].setStandardValue(20);
            esv[22].setType(-1);
            esv[23].setChangeValue(50);
            esv[23].setStandardValue(20);
            esv[23].setType(-1);
            esv[24].setChangeValue(50);
            esv[24].setStandardValue(20);
            esv[24].setType(-1);
            esv[25].setChangeValue(50);
            esv[25].setStandardValue(20);
            esv[25].setType(-1);
            esv[26].setChangeValue(50);
            esv[26].setStandardValue(20);
            esv[26].setType(-1);
            esv[27].setChangeValue(50);
            esv[27].setStandardValue(20);
            esv[27].setType(-1);
            esv[28].setChangeValue(50);
            esv[28].setStandardValue(7);
            esv[28].setType(4);
            esv[29].setChangeValue(50);
            esv[29].setStandardValue(20);
            esv[29].setType(30);
            esv[30].setChangeValue(50);
            esv[30].setStandardValue(1000);
            esv[30].setType(-1);
            esv[31].setChangeValue(50);
            esv[31].setStandardValue(5);
            esv[31].setType(0);
            esv[32].setChangeValue(50);
            esv[32].setStandardValue(1000);
            esv[32].setType(-1);
            esv[33].setChangeValue(50);
            esv[33].setStandardValue(1);
            esv[33].setType(1);
            esv[34].setChangeValue(50);
            esv[34].setStandardValue(400);
            esv[34].setType(-1);
            esv[35].setChangeValue(50);
            esv[35].setStandardValue(200);
            esv[35].setType(-1);
            esv[36].setChangeValue(50);
            esv[36].setStandardValue(70);
            esv[36].setType(-1);
            esv[37].setChangeValue(50);
            esv[37].setStandardValue(50);
            esv[37].setType(-1);
            esv[38].setChangeValue(50);
            esv[38].setStandardValue(60);
            esv[38].setType(-1);
            esv[39].setChangeValue(50);
            esv[39].setStandardValue(45);
            esv[39].setType(-1);
            for(int i = 0; i < esv.length; i++)
                s.save(esv[i]);
            
            s.flush();
            s.getTransaction().commit();
            result = true;
        }catch(Exception ex)
        {
            ex.printStackTrace();
            s.getTransaction().rollback();
        }
        finally
        {
            s.close();
        }
        
        return result;
    }

    @Override
    public boolean delete(Enterprise e) 
    {
        boolean result = false;
        
        Session s = SessionPool.getSession();
        try
        {
            s.beginTransaction();
            s.delete(e);
            s.getTransaction().commit();
            result = true;
        }catch(Exception ex)
        {
            ex.printStackTrace();
            s.getTransaction().rollback();
        }
        finally
        {
            s.close();
        }
        
        return result;
    }

    @Override
    public Enterprise getById(int id) 
    {
        Enterprise result = null;
        
        Session s = SessionPool.getSession();
        s.beginTransaction();
        
        try
        {
            result = (Enterprise) s.get(Enterprise.class, id);
            s.getTransaction().commit();
        }catch(Exception ex)
        {
            ex.printStackTrace();
            s.getTransaction().rollback();
        }
        finally
        {
            s.close();
        }
        
        return result;
    }

    @Override
    public Enterprise getByUsername(String username) 
    {
        Enterprise result = null;
        
        Session s = SessionPool.getSession();
        s.beginTransaction();
        
        try
        {
            Query q = s.createQuery("from Enterprise where username='" + username + "'");
            result = (Enterprise) q.uniqueResult();
            s.getTransaction().commit();
        }catch(Exception ex)
        {
            ex.printStackTrace();
            s.getTransaction().rollback();
        }
        finally
        {
            s.close();
        }
        
        return result;
    }

    @Override
    public boolean update(Enterprise e) 
    {
        boolean result = false;
        
        Session s = SessionPool.getSession();
        try
        {
            s.beginTransaction();
            s.update(e);
            s.getTransaction().commit();
            result = true;
        }catch(Exception ex)
        {
            ex.printStackTrace();
            s.getTransaction().rollback();
        }
        finally
        {
            s.close();
        }
        
        return result;
    }

    @Override
    public List<Enterprise> getAll() 
    {
        List<Enterprise> result = null;
        
        Session s = SessionPool.getSession();
        s.beginTransaction();
        
        try
        {
            Query q = s.createQuery("from Enterprise");
            result = (List<Enterprise>) q.list();
            s.getTransaction().commit();
        }catch(Exception ex)
        {
            ex.printStackTrace();
            s.getTransaction().rollback();
        }
        finally
        {
            s.close();
        }
        
        return result;
    }

    @Override
    public int count() 
    {
        int result = -1;
        
        Session s = SessionPool.getSession();
        s.beginTransaction();
        
        try
        {
            Query q = s.createQuery("from Enterprise");
            result = q.list().size();
            s.getTransaction().commit();
        }catch(Exception ex)
        {
            ex.printStackTrace();
            s.getTransaction().rollback();
        }
        finally
        {
            s.close();
        }
        
        return result;
    }

    @Override
    public List<Enterprise> getByTypeLevelOrderly(int enterpriseTypeID)
    {
        if(enterpriseTypeID < 0)
            return getAll();
        
        List<Enterprise> result = null;
        
        Session s = SessionPool.getSession();
        s.beginTransaction();
        try
        {
            Query q = s.createQuery("from Enterprise e where e.enterpriseType.id = " + enterpriseTypeID
                                    + " order by e.level desc");
            result = q.list();
            s.getTransaction().commit();
        }catch(Exception ex)
        {
            ex.printStackTrace();
            s.getTransaction().rollback();
        }
        finally
        {
            s.close();
        }
        
        return result;
    }
    
}
